import excel "data_application.xlsx", sheet("Sheet1") firstrow clear

// tsset
tsset DyadId Year

// peace lull
gen lull = .
replace lull = 1 if Intensity == 0
replace lull = 0 if Intensity == 1
replace lull = 0 if Intensity == 2

// gen negotiation - mediation variable
gen     NegMed = "None"
replace NegMed = "Bilateral" if Negotiation == 1 & Mediation == 0
replace NegMed = "Mediated" if  Mediation == 1

encode NegMed, gen(negmed)

// global vars
global core imppopln imprgdppcln i.RebStrBinimp 
global international Internationalized UN
global confchar Ethnic
global tsln tsln tsln2 tsln3

// generate internationalized varaible. international intervention in the last five years
gen int2 = Internationalized
replace int2 = 1 if l.Internationalized == 1
replace int2 = 1 if l2.Internationalized == 1
replace int2 = 1 if l3.Internationalized == 1
replace int2 = 1 if l4.Internationalized == 1

// Main model
global core Intensity_rate c.duration_dy_ln i.RebStr3Catimp imppopln imprgdppcln territorial c.v2x_polyarchy##c.v2x_polyarchy i.b2.Intensity total exluded2_sum coalition_sum Ethnic rebno_ct

//// labels
label variable Intensity_rate "Intensity rate"
label variable duration_dy_ln "Duration (ln)"
label variable imppopln "Population (ln)"
label variable imprgdppcln "Real GDP pc (ln)"
label variable territorial "Territorial"
label variable total "All Ethnic Groups"
label variable exluded2_sum "Excluded Ethnic Groups"
label variable coalition_sum "Ruling Ethnic Groups"
label variable Ethnic "Ethnic Rebels"
label variable rebno_ct "Number of Rebels"

// Table 2 
// model1 
logit Negotiation $core   $tsln, robust cluster(GWNoA)
est store a1
outreg2 a1 using table_2.doc, alpha(0.001, 0.01, 0.05, 0.10) symbol(***, **, *, +) stats(coef se) dec(2) replace 
estat ic

// model2
mlogit negmed $core    $tsln, robust cluster(GWNoA)
est store a2
outreg2 a2 using table_2.doc, alpha(0.001, 0.01, 0.05, 0.10) symbol(***, **, *, +) stats(coef se) dec(2) append 
estat ic


// model3
logit Mediation $core   if Negotiation == 1, robust 
est store a3
outreg2 a3 using table_2.doc, alpha(0.001, 0.01, 0.05, 0.10) symbol(***, **, *, +) stats(coef se) dec(2) append 
estat ic

// Figure 6 
// model2
mlogit negmed $core    $tsln, robust cluster(GWNoA)
margins, predict(outcome(1)) at(RebStr3Catimp = (0 (1) 2 ) ) 
marginsplot
margins, predict(outcome(2)) at(RebStr3Catimp = (0 (1) 2 ) ) 
marginsplot

